package com.xujianlong.day06;

import com.xujianlong.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

public class Flink05_KeyedState_MapState {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //3.将数据转为JavaBean
        SingleOutputStreamOperator<WaterSensor> map = streamSource.map(new MapFunction<String, WaterSensor>() {
            @Override
            public WaterSensor map(String value) throws Exception {
                String[] split = value.split(",");
                return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
            }
        });

        //4.对相同key的数据做聚合
        KeyedStream<WaterSensor, String> keyedStream = map.keyBy(r -> r.getId());

        keyedStream.process(new KeyedProcessFunction<String, WaterSensor, String>() {
            private MapState<Integer,WaterSensor> mapState;

            @Override
            public void open(Configuration parameters) throws Exception {
                mapState= getRuntimeContext().getMapState(new MapStateDescriptor<Integer, WaterSensor>("map_State", Integer.class, WaterSensor.class));
            }

            @Override
            public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {
                if(!mapState.contains(value.getVc())){
                    mapState.put(value.getVc(),value);
                }
                out.collect(mapState.get(value.getVc()).toString());
            }
        }).print();
        //mmmmm
        map.keyBy(r->r.getId()).process(new KeyedProcessFunction<String, WaterSensor, String>() {
            private MapState<Integer,WaterSensor> mapState;
            @Override
            public void open(Configuration parameters) throws Exception {
                mapState=getRuntimeContext().getMapState(new MapStateDescriptor<Integer, WaterSensor>("mapState",Integer.class,WaterSensor.class));

            }

            @Override
            public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {
                if(!mapState.contains(value.getVc())){
                    mapState.put(value.getVc(),value);
                }
                out.collect(mapState.get(value.getVc()).toString());
            }
        }).print();
        //mmmmm
        env.execute();
    }
}
